/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MetricsRetriever;

import General.DataManager;
import General.FileManager;
import DataStructures.*;
import General.Globals;

/**
 *
 * @author Manuel
 */
public class RetrievalManager extends Thread{

    private DataManager datamgr;
    private RetrieverFactory factory;
    private Project project;
    private Retriever retriever;

    public static int noSuccess = 0;
    public static int noFailedContext = 0;
    public static int noFailedByte = 0;
    public static int noFailedCode = 0;

    public RetrievalManager(DataManager dmgr){
        datamgr = dmgr;
    }


    public void run(){

        while(datamgr.getLinksLeft()>0){

            //Get the retriever according to the link
            String link = datamgr.processNextLink();
            factory = RetrieverFactory.getInstance(link);
            retriever = factory.getRetriever();
            project = new Project();
            //proceed only if everything is there (link and retriever)
            if(filter(Globals.CHECK_LINK)){

                ProjectInformation context = retriever.getContextData();
                project.setProjectInfo(context);

                //proceed only if everything is there (contextual information)
                if(filter(Globals.CHECK_CONTEXT)){

                    Metrics metrics = retriever.getMetrics();
                    project.setCodeMetrics(metrics);

                    //proceed only if the metrics where taken
                    if(filter(Globals.CHECK_CODE)){

                        RepositoryData repoData = retriever.getRepositoryData();
                        project.setRepositoryData(repoData);

                        //proceed only if repository was retrieved
                        if(filter(Globals.CHECK_REPO)){

                            BugData bugData = retriever.getBugData();
                            project.setBugData(bugData);
                            RetrievalManager.noSuccess++;
                        }
                    }
                }
            }
        }

    }

//TODO implement
    public boolean filter(int type){

        //TODO CHANGE AS SYSTEM SUPPORTS OTHER RETRIEVERS, LANGUAGES ETC...

        if(type == Globals.CHECK_CONTEXT){
            boolean hasName = !(project.getProjectInfo().getName() == null || project.getProjectInfo().getName().isEmpty());
            boolean hasLanguage = retriever.hasMetricsManager();
            boolean hasArchieveLink = !(project.getProjectInfo().getArchieveLink() == null || project.getProjectInfo().getArchieveLink().getName() == null || project.getProjectInfo().getArchieveLink().getName().isEmpty());
            boolean hasRepositoryLink = retriever.hasRepositoryManager();
            
            if(hasName && hasLanguage && hasArchieveLink && hasRepositoryLink)
                return true;

            else if(!hasName || !hasLanguage)
                RetrievalManager.noFailedContext++;
            
            else if(!hasArchieveLink)
                RetrievalManager.noFailedByte++;

            else if(!hasRepositoryLink)
                RetrievalManager.noFailedCode++;


            return false;
        }



        if(type == Globals.CHECK_CODE){
            //TODO implement, check that most important metrics are there
        }

        if(type == Globals.CHECK_REPO){
            //TODO implement, check that log and most important metrics are there
        }
        return true;
    }

    




}
